package com.evan.review.viewpager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import com.evan.review.R;

import java.util.ArrayList;
import java.util.List;

public class ImageCarouselActivity extends AppCompatActivity {
    
    private ViewPager2 viewPager;
    private TextView tvCurrentPosition;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_carousel);
        
        viewPager = findViewById(R.id.viewpager);
        tvCurrentPosition = findViewById(R.id.tv_current_position);
        
        setupCarousel();
    }
    
    private void setupCarousel() {
        // Create image data
        List<CarouselItem> carouselItems = new ArrayList<>();
        carouselItems.add(new CarouselItem(R.drawable.image_placeholder, "Mountain View"));
        carouselItems.add(new CarouselItem(R.drawable.image_placeholder, "Beach Sunset"));
        carouselItems.add(new CarouselItem(R.drawable.image_placeholder, "City Skyline"));
        carouselItems.add(new CarouselItem(R.drawable.image_placeholder, "Forest Trail"));
        carouselItems.add(new CarouselItem(R.drawable.image_placeholder, "Desert Landscape"));
        
        // Setup adapter
        CarouselAdapter adapter = new CarouselAdapter(carouselItems);
        viewPager.setAdapter(adapter);
        
        // Setup page transformations
        CompositePageTransformer transformer = new CompositePageTransformer();
        transformer.addTransformer(new MarginPageTransformer(40));
        transformer.addTransformer((page, position) -> {
            float r = 1 - Math.abs(position);
            page.setScaleY(0.85f + r * 0.15f);
        });
        
        viewPager.setPageTransformer(transformer);
        
        // Set offscreen page limit
        viewPager.setOffscreenPageLimit(3);
        
        // Setup page change callback
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                tvCurrentPosition.setText("Image " + (position + 1) + " of " + carouselItems.size());
            }
        });
        
        // Set initial text
        tvCurrentPosition.setText("Image 1 of " + carouselItems.size());
    }
    
    // Model class for carousel items
    private static class CarouselItem {
        private final int imageResId;
        private final String description;
        
        CarouselItem(int imageResId, String description) {
            this.imageResId = imageResId;
            this.description = description;
        }
        
        public int getImageResId() {
            return imageResId;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    // Adapter for the ViewPager2
    private static class CarouselAdapter extends RecyclerView.Adapter<CarouselAdapter.CarouselViewHolder> {
        
        private final List<CarouselItem> items;
        
        CarouselAdapter(List<CarouselItem> items) {
            this.items = items;
        }
        
        @NonNull
        @Override
        public CarouselViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_carousel, parent, false);
            return new CarouselViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull CarouselViewHolder holder, int position) {
            CarouselItem item = items.get(position);
            holder.imageView.setImageResource(item.getImageResId());
            holder.tvDescription.setText(item.getDescription());
        }
        
        @Override
        public int getItemCount() {
            return items.size();
        }
        
        static class CarouselViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView tvDescription;
            
            CarouselViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.image_view);
                tvDescription = itemView.findViewById(R.id.tv_description);
            }
        }
    }
} 